<template>
  <div class="sparePartList h100">
    <listTableNew v-bind="tableSetting" ref="klTable"></listTableNew>
  </div>
</template>

<script>
import {
  serchEqBackupNew, // 查询备品备件
  commitNoBindBatch,
} from "@/api/equipmentLedger/equipmentBackup.js";
export default {
  props: {
    //设备台账当前设备
    currentNode: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  components: {},
  data() {
    return {
      pageType: "list",
      tableSetting: {
        radioShow: false,
        selectionShow: true,
        showCustomBtn: false, //显示自定义列按钮
        showFilterBtn: false, //显示过滤器按钮
        rowKey: "bindId",
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
          },
          searchArr: [
            {
              type: "input",
              label: "备件编号",
              model: "code",
            },
            {
              type: "input",
              label: "备件名称",
              model: "name",
            },
            {
              type: "input",
              label: "备件型号",
              model: "type",
            },
          ],
        },
        actionList: [
          {
            name: "批量解绑",
            icon: "el-icon-setting",
            method: (row) => {
              this.batchUnbinding();
            },
          },
        ],
        column: [
          {
            label: "备件编号",
            prop: "code",
            sortable: "custom",
          },
          {
            label: "备件名称",
            prop: "name",
            sortable: "custom",
          },
          {
            label: "备件型号",
            prop: "type",
            sortable: "custom",
          },
          {
            label: "备件数量",
            prop: "amount",
            sortable: "custom",
          },
          {
            label: "设备名称",
            prop: "eqName",
            sortable: "custom",
          },
        ],
        // 表格数据
        tableData: [],
        //表格方法
        tableMethods: {
          //点击行的方法
          rowClick: (row) => {},
        },
      },
    };
  },
  created() {
    /* 添加监听事件 */
    window.addEventListener("storage", this.updateData);
  },
  beforeDestroy() {
    /* 删除监听事件 */
    window.removeEventListener("storage", this.updateData);
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.$refs.klTable.getTableConfig();
      setTimeout(() => {
        this.updateData();
      }, 1000);
    },
    updateData() {
      this.$refs.klTable.searchData();
    },
    getTableData(params, sortStr) {
      let form = { ...params };
      if (sortStr) {
        form.orders = sortStr;
      }
      form.backApply = [
        {
          equip_id: this.currentNode.id,
          equip_name: this.currentNode.name,
          level: this.currentNode.level,
          parentId: this.currentNode.parentId,
          parentName: this.currentNode.parentName,
          sort: this.currentNode.sort,
          status: this.currentNode.status,
        },
      ];
      // 调用接口请求数据
      serchEqBackupNew(form)
        .then((res) => {
          let data = res.data.list || [];
          let total = res.total || 0;
          this.$set(this.tableSetting, "tableData", data);
          this.$set(this.tableSetting, "totalNumber", total);
        })
        .catch((e) => {
          this.$set(this.tableSetting, "tableData", []);
          this.$set(this.tableSetting, "totalNumber", 0);
        });
    },
    //批量解绑
    batchUnbinding() {
      let arr = this.$refs.klTable.getSelectData();
      if (arr.length > 0) {
        this.$confirm("确定要解绑选中数据吗", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let ids = arr.map((item) => item.bindId);
            commitNoBindBatch(ids).then((res) => {
              if (res.code == 200) {
                this.$message.success("备品备件解除绑定成功");
                this.updateData();
              }
            });
          })
          .catch((e) => {});
      } else {
        this.$message.warning("请勾选至少一条数据");
      }
    },
  },
  watch: {
    currentNode: {
      handler(val) {
        this.getTableData();
      },
    },
  },
};
</script>

<style lang="less" scoped>
.sparePartChangeTime {
  position: fixed;
  bottom: 20px;
  right: 20px;
  // background-color: skyblue;
  font-size: 18px;
}
</style>
